package com.open4a.openstudy.activity.main;

import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.open4a.openstudy.R;
import com.open4a.openstudy.common.Parameters;
import com.open4a.openstudy.entity.news.NewsListEntity;
import com.open4a.openstudy.manager.api.ApiManager;

import java.io.IOException;

import common.base.BaseActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MainActivity extends BaseActivity {


    private FrameLayout mMainFragment;
    private TabLayout mMainTabLayout;
    //fragments
    private Fragment[] fragments = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListener();

        ApiManager.getInstance().newsList(Parameters.NEWS_CATALOG_ALL, 1, 20, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                NewsListEntity entity = JSON.parseObject(json, NewsListEntity.class);
            }
        });
    }

    private void setListener() {
        mMainTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mMainFragment = (FrameLayout) findViewById(R.id.main_fragment);
        mMainTabLayout = (TabLayout) findViewById(R.id.main_tabLayout);
        //底部导航按钮
        initTab(getResources().getString(R.string.news),R.drawable.nav_news);
        initTab(getResources().getString(R.string.tweet),R.drawable.nav_tweet);
        initTab(getResources().getString(R.string.discover),R.drawable.nav_discover);
        initTab(getResources().getString(R.string.my),R.drawable.nav_my);

    }

    private void initTab(String text, int Res) {
        TabLayout.Tab tab = mMainTabLayout.newTab();
        View view = getLayoutInflater().inflate(R.layout.main_nav_tab, null);
        tab.setCustomView(view);
        ImageView icon = (ImageView) view.findViewById(R.id.tab_iv);
        TextView textView = (TextView) view.findViewById(R.id.tab_tv);
        icon.setImageResource(Res);
        textView.setText(text);
        mMainTabLayout.addTab(tab);
    }

}
